package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgMainNavigationMapper;
import com.ztgf.shopping.models.bo.epg.EpgMainNavigationBO;
import com.ztgf.shopping.models.bo.epg.EpgMainNavigationSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgMainNavigationDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgMainNavigationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 主导航服务
 *
 * @author YUE
 * @create 2020-05-18 11:26
 */
@DubboService
public class EpgMainNavigationServiceImpl extends ServiceImpl<EpgMainNavigationMapper, EpgMainNavigationDTO> implements IEpgMainNavigationService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgMainNavigationMapper epgMainNavigationMapper;

    @Override
    public boolean add(EpgMainNavigationSaveBO epgMainNavigationSaveBO) {
        EpgMainNavigationDTO epgMainNavigationDTO = new EpgMainNavigationDTO();
        BeanUtils.copyProperties(epgMainNavigationSaveBO,epgMainNavigationDTO);
        Long id = ymlConfig.getPkId();
        epgMainNavigationDTO.setId(id);
        return this.insert(epgMainNavigationDTO);
    }

    /**
     * 删除功能功能相关的主导航
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgMainNavigationMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据条件查询主导航信息 有页码
     * @param epgMainNavigationBO
     * @return
     */
    @Override
    public Page<EpgMainNavigationDTO> getBySelectMainNavigationInfoPage(EpgMainNavigationBO epgMainNavigationBO) {
        epgMainNavigationBO = addMustData(epgMainNavigationBO);
        Wrapper<EpgMainNavigationDTO> wrapper = getBySelectMainNavigationInfo(epgMainNavigationBO);
        return selectPage( new Page<>(epgMainNavigationBO.getPageNo(),epgMainNavigationBO.getPageSize()),wrapper);
    }

    /**
     * 添加必要数据
     * @param epgMainNavigationBO
     * @return
     */
    private EpgMainNavigationBO addMustData(EpgMainNavigationBO epgMainNavigationBO){
        epgMainNavigationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgMainNavigationBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgMainNavigationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgMainNavigationBO;
    }

    /**
     * 根据条件查询主导航信息 无页码
     * @param epgMainNavigationBO
     * @return
     */
    @Override
    public List<EpgMainNavigationDTO> getBySelectMainNavigationInfoNoPage(EpgMainNavigationBO epgMainNavigationBO) {
        epgMainNavigationBO = addMustData(epgMainNavigationBO);
        Wrapper<EpgMainNavigationDTO> wrapper = getBySelectMainNavigationInfo(epgMainNavigationBO);
        if(epgMainNavigationBO.getPageNo() == 0 && epgMainNavigationBO.getPageSize() == 0)
            return epgMainNavigationMapper.selectPage( new Page<>(0, ShoppingConstant.PLATFORM_MAIN_NAVIGATION),wrapper);
        return selectList(wrapper);
    }

    private Wrapper<EpgMainNavigationDTO> getBySelectMainNavigationInfo(EpgMainNavigationBO epgMainNavigationBO) {
        Wrapper<EpgMainNavigationDTO> wrapper = new EntityWrapper<>();
        EpgMainNavigationDTO epgMainNavigationDTO = new EpgMainNavigationDTO();
        BeanUtils.copyProperties(epgMainNavigationBO,epgMainNavigationDTO);
        wrapper.eq("status", epgMainNavigationBO.getStatus());
        wrapper.eq("is_publish", epgMainNavigationBO.getIsPublish());
        wrapper.eq("module_id", epgMainNavigationBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgMainNavigationBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
